import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup} from "@angular/forms";
import {CamApplyService} from "../../shared/service/cam-apply.service";
import {Application} from "../../shared/model/application";
import {Attach} from "../../shared/model/attach";
import MTSConstants from "../../shared/config/config";
import {Purpose} from "../../shared/model/purpose";
import {ActivatedRoute, Router} from "@angular/router";
import {Vehicle} from "../../shared/model/vehicle";
import {Schedule} from "../../shared/model/schedule";

@Component({
  selector: 'app-cam-detail-home',
  templateUrl: './cam-detail-home.component.html',
  styleUrls: ['./cam-detail-home.component.css']
})
export class CamDetailHomeComponent implements OnInit {

  camForm:FormGroup;
  userId:string = this.routerInfo.snapshot.queryParams["userId"];
  psn:string = this.routerInfo.snapshot.queryParams["psn"];
  comeBackFlag:string = this.routerInfo.snapshot.queryParams["comeBack"];
  // psn = "PROCESSA00999";
  purposes:Purpose[];
  application:Application;
  vehicle:Vehicle;
  schedule:Schedule;
  _checked:boolean;

  constructor(
    private fb:FormBuilder,
    private camApplyService:CamApplyService,
    private routerInfo:ActivatedRoute,
    private router:Router
  ) { }

  ngOnInit() {
    this.createForm();
    this.camApplyService.getApplicationDetails(this.psn).then(res=>{
      this.clearForm();
      this.application = res.application;
      this.vehicle = res.vehicle;
      this.schedule = res.schedule;
      // console.log(res );
      if(this.application){
        this.camForm.patchValue({
          applicantId:this.application.applicant.split("_")[0],
          applicantName:this.application.applicant.split("_")[1],
          starttime:this.application.starttime,
          endtime:this.application.endtime,
          startpoint:this.application.startpoint,
          endpoint:this.application.endpoint,
          reason:this.application.reason,
          description:this.application.description
        });
      }
      if(this.vehicle){
        this.camForm.patchValue({
          vehicleName:this.vehicle.name,
          licenseNumber:this.vehicle.licenseNumber
        });
      }
      if(this.schedule){
        this.camForm.patchValue({
          mileage:this.schedule.mileageAdmin
        });
      }

    });
  }

  createForm(){
    this.camForm =this.fb.group({
      vehicleName:'',
      licenseNumber:'',
      applicantId:'',
      applicantName:'',
      starttime:'',
      endtime:'',
      startpoint:'',
      endpoint:'',
      mileage:'',
      reason:'',
      description:'',
    });
  }

  //清空formFroup
  clearForm(){
    this.camForm.reset({
      vehicleName:'',
      licenseNumber:'',
      applicantId:'',
      applicantName:'',
      starttime:'',
      endtime:'',
      startpoint:'',
      endpoint:'',
      mileage:'',
      reason:'',
      description:''
    });
  }


  comeBack(){
    if(this.comeBackFlag == "camCalendar"){
      this.router.navigate(["camCalendar/camCalendar"],{queryParams:{userId:this.userId}});
    }
    if(this.comeBackFlag == "camSearch"){
      this.router.navigate(["camSearch/camSearch"],{queryParams:{userId:this.userId}});
    }
    if(this.comeBackFlag == "camManage"){
      this.router.navigate(["camManage"],{queryParams:{userId:this.userId}});
    }
  }
}
